package com.demomaster.quickaccessibility.service;


import static com.demomaster.quickaccessibility.widget.FloatingBallMenu.SquareImageMenuView_X_SP;
import static com.demomaster.quickaccessibility.widget.FloatingBallMenu.SquareImageMenuView_Y_SP;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.demomaster.quickaccessibility.R;
import com.demomaster.quickaccessibility.widget.FloatingBallMenu;
import com.demomaster.quickcompat.content.QuickSharedPreferences;
import com.demomaster.quickcompat.service.QuickFloatingService;


/**
 * 悬浮触控按钮 返回 近期任务 桌面
 */
public class FloatingMenuService extends QuickFloatingService {
    private static FloatingBallMenu menuView;
    View view;

    @Override
    public void onCreateView(Context context, WindowManager windowManager) {
        view = LayoutInflater.from(context).inflate(R.layout.layout_floating_ball_menu, null);
        //view.setLayoutParams(new LinearLayout.LayoutParams(w,w));
        /*
        QDRoundDrawable qdRoundDrawable = new QDRoundDrawable();
        //qdRoundDrawable.setCornerRadius(cl_menu.getWidth()/2);
        qdRoundDrawable.setBackGroundColor(getResources().getColor(R.color.transparent_dark_77));
        qdRoundDrawable.setCornerRadius(DisplayUtil.dip2px(context,5));
        qdRoundDrawable.setRadiusAuto(true);
        //cl_menu.setBackground(qdRoundDrawable);*/
        menuView = view.findViewById(R.id.ib_menu_01);
        buttonEnable = true;
        menuView.setEnabled(buttonEnable);
        menuView.setWindowManager(windowManager);
//        layoutParams = new WindowManager.LayoutParams();
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;//TYPE_APPLICATION_OVERLAY;
//        } else {
//            //layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
//            layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
//        }
//        layoutParams.format = PixelFormat.RGBA_8888;
//        layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
//        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//        if (!getTouchAble()) {
//            layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//        }
//        PointF pointF = getOriginPoint();
//        if (pointF != null) {
//            layoutParams.x = (int) pointF.x;
//            layoutParams.y = (int) pointF.y;
//        }
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.width = (int) getResources().getDimension(com.demomaster.quickcompat.R.dimen.dp_45);//ViewGroup.LayoutParams.WRAP_CONTENT;
        layoutParams.height = layoutParams.width;//ViewGroup.LayoutParams.WRAP_CONTENT;
        //windowManager.addView(view, layoutParams);
        int x = QuickSharedPreferences.getInstance().getInt(SquareImageMenuView_X_SP, 0);
        int y = QuickSharedPreferences.getInstance().getInt(SquareImageMenuView_Y_SP, 200);
        addView(view, layoutParams,true, x, y);
    }
    static boolean buttonEnable = true;
    public static void setMenuEnable(boolean enable) {
        buttonEnable = enable;
        if (menuView != null) {
            menuView.setEnabled(buttonEnable);
        }
    }

    @Override
    public void onDestroy() {
        if(view!=null) {
            view.setOnTouchListener(null);
        }
        if(menuView!=null){
            menuView.setOnTouchListener(null);
            menuView = null;
        }
        removeView(view);
        super.onDestroy();
    }
}
